using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;
using System.IO;


namespace pBackup
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void closeToolStripMenuItem_Click(object sender, EventArgs e)
      {
         this.Close();
      }

      private void sevenzipToolStripMenuItem_Click(object sender, EventArgs e)
      {
         if (zipToolStripMenuItem.Checked)
            zipToolStripMenuItem.Checked = false;
         sevenzipToolStripMenuItem.Checked = true;
      }

      private void zipToolStripMenuItem_Click(object sender, EventArgs e)
      {
         if (sevenzipToolStripMenuItem.Checked)
            sevenzipToolStripMenuItem.Checked = false;
         zipToolStripMenuItem.Checked = true;
      }

      private void btnSave_Click(object sender, EventArgs e)
      {
         //Save Sources
         if (cmbSource.FindStringExact(cmbSource.Text) == ListBox.NoMatches && cmbSource.Text.Length > 0)
         {
            cmbSource.Items.Add(cmbSource.Text);
         }
         System.Collections.Specialized.StringCollection strSources = new System.Collections.Specialized.StringCollection();
         for (int ii = 0; ii < cmbSource.Items.Count; ii++)
         {
            strSources.Add(cmbSource.Items[ii].ToString());
         }
         pBackup.Properties.Settings.Default.cmbSourceItems = strSources;

         //Save Destinations
         if (cmbDestination.FindStringExact(cmbDestination.Text) == ListBox.NoMatches && cmbDestination.Text.Length > 0)
         {
            cmbDestination.Items.Add(cmbDestination.Text);
         }
         strSources = new System.Collections.Specialized.StringCollection();
         for (int ii = 0; ii < cmbDestination.Items.Count; ii++)
         {
            strSources.Add(cmbDestination.Items[ii].ToString());
         }
         pBackup.Properties.Settings.Default.cmbDestinationItems = strSources;

         //Save Filenames
         strSources = new System.Collections.Specialized.StringCollection();
         if (cmbFilename.FindStringExact(cmbFilename.Text) == ListBox.NoMatches && cmbFilename.Text.Length > 0)
         {
            cmbFilename.Items.Add(cmbFilename.Text);
         }
         for (int ii = 0; ii < cmbFilename.Items.Count; ii++)
         {
            strSources.Add(cmbFilename.Items[ii].ToString());
         }
         pBackup.Properties.Settings.Default.cmbFilenameItems = strSources;
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         for (int ii = 0; ii < pBackup.Properties.Settings.Default.cmbSourceItems.Count; ii++)
         {
            cmbSource.Items.Add(pBackup.Properties.Settings.Default.cmbSourceItems[ii]);
         }
         for (int ii = 0; ii < pBackup.Properties.Settings.Default.cmbDestinationItems.Count; ii++)
         {
            cmbDestination.Items.Add(pBackup.Properties.Settings.Default.cmbDestinationItems[ii]);
         }
         for (int ii = 0; ii < pBackup.Properties.Settings.Default.cmbFilenameItems.Count; ii++)
         {
            cmbFilename.Items.Add(pBackup.Properties.Settings.Default.cmbFilenameItems[ii]);
         }
      }

      private void cmbSource_SelectedIndexChanged(object sender, EventArgs e)
      {
         cmbSource.Focus();
         if (cmbSource.SelectedText == "Browse...")
         {
            if (sourceFolderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
               cmbSource.Items.Add(sourceFolderBrowserDialog.SelectedPath);
               int index = cmbSource.FindStringExact(sourceFolderBrowserDialog.SelectedPath);
               cmbSource.SelectedIndex = index;
            }
            else
            {
               cmbSource.SelectedIndex = -1;
            }
         }
      }

      private void cmbDestination_SelectedIndexChanged(object sender, EventArgs e)
      {
         cmbDestination.Focus();
         if (cmbDestination.SelectedText == "Browse...")
         {
            if (sourceFolderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
               cmbDestination.Items.Add(sourceFolderBrowserDialog.SelectedPath);
               int index = cmbDestination.FindStringExact(sourceFolderBrowserDialog.SelectedPath);
               cmbDestination.SelectedIndex = index;
            }
            else
            {
               cmbDestination.SelectedIndex = -1;
            }
         }
      }

      private void btnBackup_Click(object sender, EventArgs e)
      {
         //program name
         string path = @"c:\Program Files\7-Zip\7z";
         string cmd = "\"" + path + "\"";
         string options = "";
         string destination = "";
         string source = "";
         if (true)
         {
            //command
            options = "a"; //a for add or append archive
            //switches
            if (zipToolStripMenuItem.Checked)
            {
               options += " -tzip";
            }
            else if (sevenzipToolStripMenuItem.Checked)
            {
               options += " -t7z";
            }

            //destination
            destination = " \"" + cmbDestination.Text + "\\" + cmbFilename.Text;
            System.DateTime date = System.DateTime.Now;
            if (chkDate.Checked)
            {
               destination += "_" + date.Year + date.Month.ToString().PadLeft(2, '0') + date.Day.ToString().PadLeft(2, '0');
            }
            if (chkTime.Checked)
            {
               destination += "_" + date.Hour.ToString().PadLeft(2, '0') + date.Minute.ToString().PadLeft(2, '0');
            }
            if (chkSeconds.Checked)
            {
               destination += date.Second.ToString().PadLeft(2, '0');
            }
            if (zipToolStripMenuItem.Checked)
            {
               destination += ".zip\"";
            }
            else if (sevenzipToolStripMenuItem.Checked)
            {
               destination += ".7z\"";
            }

            //source
            source = " \"" + cmbSource.Text + "\"";
         }
         RunAtCmd(cmd, options + destination + source);
         lblStatus.Text = "Compressing...";
      }

      private void chkSeconds_CheckedChanged(object sender, EventArgs e)
      {
         if (chkSeconds.Checked)
            chkTime.Checked = true;
      }

      private void chkTime_CheckedChanged(object sender, EventArgs e)
      {
         if (!chkTime.Checked)
            chkSeconds.Checked = false;
      }

      private void workerThread_DoWork(object sender, DoWorkEventArgs e)
      {
         try
         {
            Process p = new Process();
            p.StartInfo = (ProcessStartInfo)e.Argument;
            p.Start();
            p.WaitForExit();
         }
         catch (Win32Exception ex)
         {
            MessageBox.Show(ex.Message);
         }
         catch (ApplicationException ex)
         {
            MessageBox.Show(ex.Message);
         }
      }

      void workerThread_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
      {
         lblStatus.Text = "";
      }

      private void RunAtCmd(string filename, string arguments)
      {
         ProcessStartInfo psi = new ProcessStartInfo();
         psi.FileName = filename;
         psi.Arguments = arguments;
         psi.UseShellExecute = true;
         workerThread.RunWorkerAsync(psi);
      }
   }
}